<template>
</template>

<script>
import siteinfo from "./siteinfo.js";
var awgaFun = require("./function.js");
var rewardedVideoAd = null;//激励视频广告对象
var rewardedVideoId = null;//定义看广告标识
export default{
	name: "haichongAdVideo",
	data() {
		return {
			adUnitId: siteinfo.adVideo,//激励视频广告ID
			//广告状态（null未加载）（ok成功）（no失败）（loading加载中）
			loadState: null
		}
	},
	created(){
		this.$nextTick(() => {
			this.loadingVideoAd();
		})
	},
	methods: {
		loadingVideoAd(){
			if(!this.adUnitId){
				console.log("没有配置视频广告ID");
				return;
			}
			var _me = this;
			rewardedVideoAd = uni.createRewardedVideoAd({adUnitId:_me.adUnitId});
			rewardedVideoAd.onLoad(() => {console.log("激励视频广告加载成功");
				_me.adState('ok');
			});
			rewardedVideoAd.onError((a) => {
				_me.adError(a,false);
				_me.adState('no');
			});
			rewardedVideoAd.onClose((res) => {
				_me.adState(null);
			    if (res && res.isEnded) {//正常播放结束
			        _me.rewardedVideoAdPost();
			    } else {// 播放中途退出
					_me.adState(null);
			        uni.showToast({title: "看完视频才有奖励哦~",icon: "none"});
			    }
				setTimeout(() => {
					if(_me.loadState===null){
						_me.adState('no');
						rewardedVideoAd.load();
					}
				},10000);//十秒没加成成功，默认加载失败，显示重载按钮
			});
			setTimeout(() => {
				if(_me.loadState===null){
					_me.adState('no');
					rewardedVideoAd.load();
				}
			},10000);//十秒没加成成功，默认加载失败，显示重载按钮
		},
		//显示广告
		showAd(e) {
			if(!e){console.log("未定义看视频广告标识");return;}
			this.adState('loading');
			rewardedVideoId = e;//广告标识
			var _me = this;
			rewardedVideoAd.show().catch(() => {
				rewardedVideoAd.load()//显示失败重新加载
				.then(() => {//重新加成成功则显示
					rewardedVideoAd.show()
				})
				.catch(err => {//重新加载失败则
					_me.adError(err,true);
					_me.adState('no');
				})
			});
		},
		adState(e){
			this.loadState = e;
			this.$emit('loadState',e);
		},
		reloadAd() {
			if(this.loadState == 'no'){
				this.loadState = null;
				rewardedVideoAd.load();
			}
			this.adState();
		},
		rewardedVideoAdPost(){
			
			this.$emit('adData',rewardedVideoId);
			this.adState(null);
			
			awgaFun.post('video',{},function(res){});
			
			// var _me = this;
			// if(!rewardedVideoId){
			// 	uni.showToast({title: "系统错误",icon: "none"});
			// 	console.log("没有广告标识 rewardedVideoId 是空");
			// 	return;
			// }
			// uni.showLoading({
			// 	mask: true,
			//     title: '加载中...'
			// });
			// _me.adState(null);
			// wx.login({
			// 	success (res) {
			// 		awgaFun.post({
			// 			apiType: '_api_advideo',
			// 			videoId: rewardedVideoId,
			// 			code: res.code
			// 		},  function(res){
			// 				uni.hideLoading();
			// 				if(res.errno == 1){
			// 					_me.$emit('adData',{
			// 						adId:rewardedVideoId,
			// 						adDt:res.data
			// 					});
			// 				}else{
			// 					uni.showToast({icon:'none',title:res.message,duration: 2000});
			// 				}
			// 			}
			// 		)
			// 	},
			// 	fail(e) {
			// 		uni.hideLoading();
			// 	    uni.showToast({
			// 	    	icon: 'none',
			// 	        title: '获取登录凭证失败',
			// 	        duration: 2000
			// 	    });
			// 	}
			// });
			
			
			
		},
		adError(a,tips){
			if (a.errCode == 1000) {
				console.log("该项错误不是开发者的异常情况");
				if(tips){uni.showToast({title: "后端错误调用失败",icon: "none"});}
			} else if (a.errCode == 1001) {
				console.log("使用方法错误");
				if(tips){uni.showToast({title: "参数错误",icon: "none"});}
			} else if (a.errCode == 1002) {
				console.log("可能是拼写错误、或者误用了其他APP的广告ID");	
				if(tips){uni.showToast({title: "广告单元无效",icon: "none"});}
			} else if (a.errCode == 1003) {
				console.log("该项错误不是开发者的异常情况");
				if(tips){uni.showToast({title: "内部错误",icon: "none"});}
			} else if (a.errCode == 1004) {
				console.log("广告不是每一次都会出现，这次没有出现可能是由于该用户不适合浏览广告");
				if(tips){uni.showToast({title: "暂无适合的视频",icon: "none"});}
			} else if (a.errCode == 1005) {
				console.log("你的广告正在被审核，无法展现广告");	
				if(tips){uni.showToast({title: "广告组件审核中",icon: "none"});}
			} else if (a.errCode == 1006) {
				console.log("你的广告审核失败，无法展现广告");
				if(tips){uni.showToast({title: "广告组件被驳回",icon: "none"});}
			} else if (a.errCode == 1007) {
				console.log("你的广告能力已经被封禁，封禁期间无法展现广告");	
				if(tips){uni.showToast({title: "广告组件被驳回",icon: "none"});}
			} else if (a.errCode == 1008) {
				console.log("该广告位的广告能力已经被关闭");
				if(tips){uni.showToast({title: "广告单元已关闭",icon: "none"});}
			}
		}
	}
}
</script>

<style>
</style>
